package com.yj.springsecuritydemo01.rest;

import com.yj.springsecuritydemo01.mapper.UserMapper;
import com.yj.springsecuritydemo01.pojo.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private PasswordEncoder passwordEncoder;


    @PostMapping("register")
    public String register(@RequestBody UserInfo userInfo){

        if (StringUtils.isBlank(userInfo.getUsername()) && StringUtils.isBlank(userInfo.getPassword())){
            return "username or password was error!";
        }
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        userMapper.register(userInfo);
        return "register success!";
    }


    @PostMapping("login")
    public String login(@RequestBody UserInfo userInfo){
        if (StringUtils.isBlank(userInfo.getUsername()) && StringUtils.isBlank(userInfo.getPassword())){
            return "username or password was error!";
        }
        return "login success!";
    }

    @GetMapping("find")
    public List find(){
        List<UserInfo> list = userMapper.find();
        return list;
    }

}
